package com.example.smart_city_0829.activity.lawyer_consultation;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.smart_city_0829.R;
import com.example.smart_city_0829.base.BaseActivity;
import com.example.smart_city_0829.base.CommonAdapter;
import com.example.smart_city_0829.entity.lawyer_onsultation.Consultation;
import com.example.smart_city_0829.entity.lawyer_onsultation.Lawyer;
import com.example.smart_city_0829.retrofit.NetCallBack;
import com.example.smart_city_0829.utils.Constant;
import com.example.smart_city_0829.view.MaxSizeGv;
import com.google.gson.reflect.TypeToken;

import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.stream.Collectors;

import retrofit2.Call;
import retrofit2.Response;

public class ConsultationDetailActivity extends BaseActivity {

    private ImageView im;
    private TextView name;
    private TextView workYearsTv;
    private TextView goodAtTv;
    private TextView serverTimeTv;
    private TextView favorableRateTv;
    private TextView stateTv;
    private TextView typeTv;
    private TextView contentTv;
    private TextView nonePicTv;
    private MaxSizeGv imGv;
    private TextView phoneTv;
    private Button rateBt;



    @Override
    protected int getLayoutId() {
        return R.layout.activity_consultation_detail;
    }

    @Override
    protected void initView() {
        im = (ImageView) findViewById(R.id.im);
        name = (TextView) findViewById(R.id.name);
        workYearsTv = (TextView) findViewById(R.id.work_years_tv);
        goodAtTv = (TextView) findViewById(R.id.goodAt_tv);
        serverTimeTv = (TextView) findViewById(R.id.server_time_tv);
        favorableRateTv = (TextView) findViewById(R.id.favorableRate_tv);
        stateTv = (TextView) findViewById(R.id.state_tv);
        typeTv = (TextView) findViewById(R.id.type_tv);
        contentTv = (TextView) findViewById(R.id.content_tv);
        nonePicTv = (TextView) findViewById(R.id.none_pic_tv);
        imGv = (MaxSizeGv) findViewById(R.id.im_gv);
        phoneTv = (TextView) findViewById(R.id.phone_tv);
        rateBt = (Button) findViewById(R.id.rate_bt);
    }

    @Override
    protected void initData() {
        Consultation.RowsBean data = gson.fromJson(getIntent().getStringExtra("data"),new TypeToken<Consultation.RowsBean>(){}.getType());

        if (data != null) {
            //TODO 加载律师详细信息
            api.getLawyer(data.getLawyerId()).enqueue(new NetCallBack<Lawyer>() {
                @Override
                public void onResponse(Call<Lawyer> call, Response<Lawyer> response) {
                    if (response.body() == null) return;
                    final Lawyer.DataBean lawyer = response.body().getData();
                    Glide.with(context)
                            .load(Constant.BASE_URL + lawyer.getAvatarUrl())
                            .into(im);

                    name.setText(String.valueOf(lawyer.getName()));
                    workYearsTv.setText("从业"+ (Calendar.getInstance().get(Calendar.YEAR) - Integer.parseInt(lawyer.getWorkStartAt().substring(0,4)) + "年"));
                    goodAtTv.setText("法律专长："+ lawyer.getLegalExpertiseName());
                    serverTimeTv.setText("咨询人数："+lawyer.getServiceTimes());
                    favorableRateTv.setText("h好评率："+lawyer.getFavorableRate()+"%");
                }
            });
            //TODO 显示咨询详情
            stateTv.append(Constant.getConsultationState(Integer.parseInt(data.getState())));
            if ("2".equals(data.getState())){
                rateBt.setVisibility(View.VISIBLE);
            }else {
                rateBt.setVisibility(View.GONE);
            }
            typeTv.append(data.getLegalExpertiseName());
            contentTv.append(data.getContent());
            phoneTv.append(data.getPhone());
            final String[] imUrlsSplits = data.getImageUrls().split(",");
            if (imUrlsSplits.length == 0){
                nonePicTv.setVisibility(View.VISIBLE);
            }else {
                imGv.setVisibility(View.VISIBLE);
                imGv.setAdapter(new GvAdapter(
                        Arrays.stream(imUrlsSplits).collect(Collectors.toList()),
                        context
                ));
            }
        }


    }

    @Override
    protected void initEvent() {
        rateBt.setOnClickListener(v -> {
            final Intent intent = new Intent(context, EvaluationActivity.class);
            intent.putExtra("data",getIntent().getStringExtra("data"));
            startActivity(intent);
        });
    }

    @Override
    protected String getActionBarTitle() {
        return "咨询详情";
    }

    class GvAdapter extends CommonAdapter<String,CommonAdapter.ImViewHolder> {


        public GvAdapter(List<String> data, Context context) {
            super(data, context);
        }

        @Override
        protected void setData(String data, int position, ImViewHolder viewHolder, View convertView, Context context) {
            Glide.with(context)
                    .load(Constant.BASE_URL + "prod-api" + data)
                    .into(viewHolder.im);
        }

        @Override
        protected ImViewHolder getViewHolder() {
            return new ImViewHolder();
        }

        @Override
        protected void initView(ImViewHolder viewHolder, View convertView) {
            viewHolder.im = convertView.findViewById(R.id.im);
        }

        @Override
        protected int getItemLayoutResourceId() {
            return R.layout.im_item;
        }
    }




}